//
//  KKUser.m
//  ReplyAssistant
//
//  Created by Leo on 2018/11/23.
//  Copyright © 2018 KK. All rights reserved.
//

#import "KKUser.h"

#define KKUser_Nickname @"KKUser_Nickname"
#define KKUser_Headimg @"KKUser_Headimg"
#define KKUser_Ticket @"KKUser_Ticket"
#define KKUser_Uid @"KKUser_Uid"
#define KKUser_Status @"KKUser_Status"
#define KKUser_Team @"KKUser_Team"

@interface KKUser() {
    NSString *_ticket;      //  授权id
    NSString *_uid;         //  用户id
}

@end

@implementation KKUser
+ (instancetype)defaultUser {
    static KKUser *user = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        user = [KKUser new];
    });
    return user;
}

+ (void)loginWithJson:(NSDictionary*)json{
    [self loginWithJson: json nickName: nil headImg: nil];
}

+ (void)loginWithJson:(NSDictionary *)json nickName:(NSString*)nickname headImg:(NSString*)headImg {
    if ([json isKindOfClass: [NSDictionary class]]) {
        NSString *ticket = json[@"ticket"];
        NSString *uid = json[@"uid"];
        NSString *tmp_nickname = json[@"nick"];
        NSString *tmp_headimg = json[@"headimg"];
        
        NSString *team = json[@"team"];
        
        if (tmp_nickname == nil) {
            tmp_nickname = nickname;
        }
        
        if (tmp_headimg == nil) {
            tmp_headimg = headImg;
        }
        
        if ([ticket isKindOfClass: [NSString class]]) {
            KKUser.defaultUser->_ticket = ticket;
        }
        
        KKUser.defaultUser->_uid = uid;
        KKUser.defaultUser->_headimg = tmp_headimg;
        KKUser.defaultUser->_nickName = tmp_nickname;
        KKUser.defaultUser->_team = team;
        
        if (ticket != nil && uid != nil) {
            [self synchroData];
        }
        
        if (KKUser.defaultUser.updateBlock != nil) {
            KKUser.defaultUser.updateBlock(true);
        }
    }
}

//  加载详情信息
+ (void)loadingUserDefailInfoWithJson:(NSDictionary*)json {
    if ([json isKindOfClass: [NSDictionary class]]) {
        NSString *nickname = json[@"nick"];
        NSString *headimg = json[@"headimg"];
        bool status = [json[@"status"] boolValue];
        NSInteger message = [json[@"message"] integerValue];
        NSInteger customer = [json[@"customer"] integerValue];
        
        NSString *team = json[@"team"];
        
        KKUser.defaultUser->_nickName = nickname;
        KKUser.defaultUser->_headimg = headimg;
        KKUser.defaultUser->_online = !status;
        KKUser.defaultUser->_message = message;
        KKUser.defaultUser->_customer = customer;
        KKUser.defaultUser->_team = team;
    }
}

//  同步读取数据
+ (void)synchronizationReadData {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    KKUser.defaultUser->_ticket = [userDefaults stringForKey: KKUser_Ticket];
    KKUser.defaultUser->_uid = [userDefaults stringForKey: KKUser_Uid];
    KKUser.defaultUser->_headimg = [userDefaults stringForKey: KKUser_Headimg];
    KKUser.defaultUser->_nickName = [userDefaults stringForKey: KKUser_Nickname];
    
    KKUser.defaultUser->_team = [userDefaults stringForKey: KKUser_Team];
    KKUser.defaultUser->_online = [[userDefaults stringForKey: KKUser_Status] boolValue];
}

//  同步保存数据
+ (void)synchroData {
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    KKUser *user = [KKUser defaultUser];
    if (user->_ticket == nil) {
        [userDefaults removeObjectForKey: KKUser_Ticket];
        [userDefaults removeObjectForKey: KKUser_Uid];
        [userDefaults removeObjectForKey: KKUser_Nickname];
        [userDefaults removeObjectForKey: KKUser_Headimg];
        [userDefaults removeObjectForKey: KKUser_Team];
        [userDefaults removeObjectForKey: KKUser_Status];
    }
    else {
        [userDefaults setValue: user->_ticket forKey: KKUser_Ticket];
        [userDefaults setValue: user->_uid forKey: KKUser_Uid];
        [userDefaults setValue: user.nickName forKey: KKUser_Nickname];
        [userDefaults setValue: user.headimg forKey: KKUser_Headimg];
        [userDefaults setValue: user.team forKey: KKUser_Team];
        [userDefaults setValue: @(user.online) forKey: KKUser_Status];
    }
    [userDefaults synchronize];
}

+ (void)exit {
    KKUser *user = [KKUser defaultUser];
    user.nickName = nil;
    user.headimg = nil;
    user.online = false;
    user->_ticket = nil;
    user->_uid = nil;
    
    [self synchroData];

    if (user.updateBlock != nil) {
        user.updateBlock(false);
    }
}

+ (NSString*)ticket {
    return [KKUser defaultUser]->_ticket;
}

+ (NSString*)uid {
    return [KKUser defaultUser]->_uid;
}

+ (BOOL)isLogin {
    if ([self ticket]) {
        return true;
    }
    else {
        return false;
    }
}

@end
